<?php 

if (! defined('BASEPATH')) exit('No direct script access');

class user_model extends CI_Model {

   
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
    public function login($username = FALSE, $password = FALSE)
    {
        if ($username === FALSE OR $password === FALSE) 
        {
            return FALSE;
        }
        /*$query = $this->db->select('id, username, password')
                          ->where('username', $username)
                          ->limit(1)
                          ->get('users');
        */
        $query = $this->db->select('*')
                 ->from('user')
                 ->limit(1)
                 ->get();
        $result = $query->row();
        
        
        if ($query->num_rows() == 1) 
        {
            $password = md5($password);
            if($result->password == $password)
            {
                $this->session->set_userdata('id', $result->id);     
                $this->session->set_userdata('email', $result->email);  
                $this->session->set_userdata('firstname', $result->firstname); 
                $this->session->set_userdata('lastname', $result->lastname); 
                return TRUE;
            }
        }
        
        return FALSE;
    }
    
    function create($email,$password,$firstname,$lastname,$address)
    {
        $query = $this->db->select('*')
                 ->from('user')
                 ->where('email', $email)
                 ->limit(1)
                 ->get();
        $result = $query->row();
        
        if ($query->num_rows() == 1) {
            return false;
        }
        
        $user = array(
                      'email'           => $email,    
                      'password'        => md5($password),
                      'firstname'       => $firstname,
                      'lastname'        => $lastname,
                      'address'           => $email,
                     );
                     
        $this->db->insert('user', $user);
        $user_id =$this->db->insert_id();    
        $this->session->set_userdata('id', $user_id);  
        $this->session->set_userdata('email', $email);  
        $this->session->set_userdata('firstname', $firstname); 
        $this->session->set_userdata('lastname', $lastname);   
        return  $user_id;
        
    }
    
    function get_by_id($user_id)
    {
        $query = $this->db->select('*')
                                      ->where('id',$user_id)
                                      ->limit(1)
                                      ->get('users');
        $result = $query->row();
        return $result;
    }
    

    
    function get_all($num = FALSE, $offset = FALSE) 
    {
        $this->db->select('users.*,roles.name as role_name');
        $this->db->join('roles', 'roles.id = users.roles_id');
        $query = $this->db->get('users', $num, $offset);
        return $query->result();
    }
    
    function update_role($user_id = FLASE, $roles_id = FALSE)
    {
        if ($user_id === FALSE OR $roles_id === FALSE) 
        {
            return FALSE;
        }
        $user = array('roles_id' => $roles_id);
        $this->db->where('id', $user_id);
        $this->db->update('users', $user);
        return TRUE;
    }

}

/* End of file user_model.php */
/* Location: ./system/application/models/user_model.php */